package com.birth.memo.service;



import java.util.Calendar;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.birth.memo.db.ConfigCenter;
import com.birth.memo.util.Constants;


public class BootCompletedReceiver extends BroadcastReceiver {
	
   public  static boolean isRunning;
   
   public void onReceive(Context context, Intent intent) {
	   
	   Log.d("BootCompleted", "china hot search begin alarm service!");
	   
	   if(!isRunning){
		   isRunning = true;
		   checkBirthday(context);
	   }
	    
   }
   
   public static void checkBirthday(Context context){
	   if(ConfigCenter.getValue(context, Constants.KEY_ENABLE_NOTIFICATION, true)){
			 //启动定时检查
			   Calendar c = Calendar.getInstance();
			   int expectedHour = 8;
			   int nowHour = c.get(Calendar.HOUR_OF_DAY);
			   if(nowHour >=expectedHour){
				   c.add(Calendar.DAY_OF_MONTH, 1);
			   }
			   c.set(Calendar.HOUR_OF_DAY, expectedHour);
			   c.set(Calendar.MINUTE, 0);

			   /* 指定闹钟设置时间到时要运行CallAlarm.class */
		       Intent i = new Intent(context,CheckNotification.class);
		       
		       PendingIntent sender = PendingIntent.getBroadcast(context,1, i, 0);
		       /* setRepeating()可让闹钟重复运行 */
		       AlarmManager am;
		       
		       //间隔时间
		       int times= 24*60*60*1000;
		       am = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
		       am.setRepeating(AlarmManager.RTC_WAKEUP,
		                 c.getTimeInMillis(),times,sender);
		   }    
   }

}